package com.mengxuegu.springboot;


import com.mengxuegu.springboot.feng.FileController;
import org.junit.Test;

import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.test.context.junit4.SpringRunner;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

@RunWith(SpringRunner.class)
@SpringBootTest
public class Utils {
    Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    com.mengxuegu.springboot.feng.Emp emp;

    @Autowired
    FileController fileController;


    @Test
    public void a(){
        logger.info("fffffdddf");

        //通过@ConfigurationProperties(prefix = "emp")方法 从配置文件获取数据
        System.out.println(emp);
        //通过@Value("${FilePath}")方法  从配置文件获取数据
        String filePath = fileController.getFilePath();
        System.out.println(filePath);

    }


    @Autowired
    JavaMailSenderImpl javaMailSender;

    //发送简单邮件
    @Test
    public void email(){
        //封装简单的邮箱信息
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        simpleMailMessage.setSubject("sss");
        simpleMailMessage.setText("sss");
        simpleMailMessage.setFrom("2602508304@qq.com");
        simpleMailMessage.setTo("2602508304@qq.com");
        javaMailSender.send(simpleMailMessage);
    }


    //发送复杂的邮件
    @Test
    public void testMimeMail() throws MessagingException {
        //封装简单的邮件内容
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();

        //通过消息帮助对象来设置发送的内容 ,第二个参数为true表示可以发送附件
        MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true);

        //标题
        messageHelper.setSubject("放假通知");
        //第2个参数为true,才可以发送html代码
        messageHelper.setText("<h2 style='color:red'>春节放假7天</h2>", true);

        //发送附件
        messageHelper.addAttachment("1.jpg", new File("c:\\upload\\1.jpg"));
        messageHelper.addAttachment("2.jpg", new File("c:\\upload\\2.jpg"));
        messageHelper.addAttachment("c.jpg", new File("c:\\upload\\3.jpg"));
        //发件人
        messageHelper.setFrom("2602508304@qq.com");
        //收件人
        messageHelper.setTo("2602508304@qq.com");

        javaMailSender.send(mimeMessage);

    }

}
